﻿
local L = LibStub("AceLocale-3.0"):GetLocale("E_EM")
local media = LibStub("LibSharedMedia-3.0")
local textures = media:List("statusbar")
local fonts = media:List("font")

local function GetLSMIndex(t, value)
	for k, v in pairs(media:List(t)) do
		if v == value then
			return k
		end
	end
	return nil
end

E_EM.options = { 
	type = "group", 
	name = "GPLP Statistic Meter", 
	args = {
	general = {
			type = "group",
			inline = true,
			name = L["General"],
			order = 1,
			args = {
				mdps = {
					type = "toggle",
					name = L["Medidor de DPS"],
					desc = "|cffff2222"..L["Es necesario relogear o ejecutar /console reloadui"].."|r",
					order = 1,
					get = function() return E_EM.db.global.medps end,
					set = function()
						E_EM.db.global.medps = not E_EM.db.global.medps
						E_EM.db.global.meheal = not E_EM.db.global.meheal
						DEFAULT_CHAT_FRAME:AddMessage("|cffff2222"..L["Es necesario relogear o ejecutar /console reloadui"].."|r");
					end,
				},	
				mheal = {
					type = "toggle",
					name = L["Medidor de Heal"],
					desc = "|cffff2222"..L["Es necesario relogear o ejecutar /console reloadui"].."|r",
					order = 2,
					get = function() return E_EM.db.global.meheal end,
					set = function()
						E_EM.db.global.medps = not E_EM.db.global.medps
						E_EM.db.global.meheal = not E_EM.db.global.meheal
						DEFAULT_CHAT_FRAME:AddMessage("|cffff2222"..L["Es necesario relogear o ejecutar /console reloadui"].."|r");
					end,
				},	
				overdmg = {
					type = "toggle",
					name = L["Contador Daño Extra"],
					desc = " ",
					order = 3,
					get = function() return E_EM.db.global.ovrdmg end,
					set = function()
						E_EM.db.global.ovrdmg = not E_EM.db.global.ovrdmg
						DEFAULT_CHAT_FRAME:AddMessage("|cffff2222"..L["Es necesario relogear o ejecutar /console reloadui"].."|r");
					end,
				},	
				lock = {
					type = "toggle",
					name = L["Bloquear Ventana"],
					desc = " ",
					order = 3,
					get = function() return E_EM.db.global.lock end,
					set = function()
						E_EM.db.global.lock = not E_EM.db.global.lock
					end,
				},
				interval = { 
					type = 'range',
					name = L["Intervalo de Actualizacion"],
					desc = "|cffff2222"..L["Ten cuidado, afecta al rendimiento!"].."|r",
					min = 0.1,
					max = 10,
					step = 0.1,
					get = function() return E_EM.db.global.interval end,
					set = function(info, v) E_EM.db.global.interval = v;
						local t=GetTime()
						E_EM.MainFrame:SetScript("OnShow", function() 
							dbd.vis=true
							E_EM.MainFrame:SetScript('OnUpdate', function() 
								if GetTime()-t<v then return end 
								t=GetTime()
								E_EM:UpdateFrame() 
							end) 
						end)
						E_EM.MainFrame:Hide()
						E_EM.MainFrame:Show()				 
					end, 
					order = 6, 
				},
			},
		},	
		FuBar = {
			order = 110,
			inline = true,
			type = "group",
			name = L["FuBar Options"],
			desc = L["FuBar Options"],
			hidden = function() return not IsAddOnLoaded("FuBar") end,
			args = {		
				showIcon = {
					type = "toggle",
					order = 3,
					name = L["Show icon"],
					desc = L["Show icon"],
					get = function(info) return E_EM:IsFuBarIconShown() end,
					set = function(info, v) E_EM:ToggleFuBarIconShown() end,
				},
				showText = {
					type = "toggle",
					order = 4,
					name = L["Show text"],
					desc = L["Show text"],
					get = function(info) return E_EM:IsFuBarTextShown() end,
					set = function(info, v) E_EM:ToggleFuBarTextShown() end,					
				},
				pos = {
					type = "select",
					name = "Position",
					desc = "Position",
					values = {["LEFT"]="Left", ["RIGHT"]="Right", ["CENTER"]="Center"},
					get = function(info) return E_EM:GetPanel():GetPluginSide(E_EM) end,
					set = function(info, v)
						if E_EM:GetPanel() and E_EM:GetPanel().SetPluginSide then
							E_EM:GetPanel():SetPluginSide(E_EM, v)
						end
					end,
				},					
			},
		},
		Appearance = {
			type = "group",
			inline = true,
			name = L["Apariencia"],
			order = 2,
			args = {		
				test = { 
					type = 'execute',
					name = "Lanzar Ventana",
					desc = L["Muestra la ventana de la herramienta."],
					func = function() E_EM:OnCmd("lanzar") end,					
					order = 1, 
				},						 
				height = { 
					type = 'range',
					name = L["Altura de Barra"],
					desc = L["Cambia la altura de las barras."],
					min = 6,
					max = 25,
					step = 1,
					get = function() return E_EM.db.global.height end,
					set = function(info, v) E_EM.db.global.height = v;
						local count = 0 
						for i=1,#E_EM.bars do
							E_EM.bars[i]:SetHeight(v);
							E_EM.bars[i]:ClearAllPoints()
							E_EM.bars[i]:SetPoint("TOPLEFT",8,(i*-E_EM.db.global.height)+E_EM.db.global.height-7)
							if E_EM.bars[i]:IsVisible() then count=count+1 end
						end 
						E_EM.container:SetHeight((count*E_EM.db.global.height)+13)
						E_EM.MainFrame:SetVerticalScroll(E_EM.db.global.height) 
						E_EM.MainFrame:SetScale(E_EM.db.global.scale)
					end,
					order = 3, 
				},
				width = { 
					type = 'range',
					name = L["Anchura de Barra"],
					desc = L["Cambia el ancho de las barras."],
					min = 230,
					max = 500,
					step = 1,
					get = function() return E_EM.db.global.width end,
					set = function(info, v) E_EM.db.global.width = v; 
						if E_EM.db.global.scrollbar==true then 
							E_EMMainFrameScrollBar:SetScale(0.7)
							for i=1,#E_EM.bars do
								E_EM.bars[i]:SetWidth(v-27)
							end
						else 
							E_EMMainFrameScrollBar:SetScale(0.00000001)
							for i=1,#E_EM.bars do
								E_EM.bars[i]:SetWidth(v-15)
							end							
						end
						E_EM.MainFrame:SetWidth(v)
						E_EM.header:SetWidth(v) 
						E_EM.footer:SetWidth(v)
						E_EM.MainFrame:SetUserPlaced(true)
						E_EM.MainFrame:UpdateScrollChildRect()
					end,
					order = 4, 
				},				
				fontsize = { 
					type = 'range',
					name = L["Tamaño de la fuente"],
					desc = L["Cambiar Fuente."],
					min = 6,
					max = 20,
					step = 1,
					get = function() return E_EM.db.global.fontsize end,
					set = function(info, v) E_EM.db.global.fontsize = v;
						for i=1,#E_EM.bars do
							E_EM.Index[i]:SetFont(media:Fetch("font",E_EM.db.global.font),v)	
							E_EM.Index[i]:ClearAllPoints()
							E_EM.Index[i]:SetPoint("LEFT", E_EM.bars[i], 4, 0)
							E_EM.LeftText[i]:SetFont(media:Fetch("font",E_EM.db.global.font),v)													 
							E_EM.LeftText[i]:ClearAllPoints()
							E_EM.LeftText[i]:SetPoint("LEFT", E_EM.bars[i], 24+v, 0);
							E_EM.RightText[i]:SetFont(media:Fetch("font",E_EM.db.global.font),v)													 
							E_EM.RightText[i]:ClearAllPoints(); 
							E_EM.RightText[i]:SetPoint("RIGHT", E_EM.bars[i], -4, 0);													 
						end										 
					end, 
					order = 5, 
				},											 	
				colorbg = { 
					type = 'color',
					name = L["Color de Fondo"],
					desc = L["Cambiar color de fondo y opacidad."],
					hasAlpha = true,
					get = function(info) return E_EM.db.global.colorbg.r, E_EM.db.global.colorbg.g, E_EM.db.global.colorbg.b, E_EM.db.global.colorbg.a end,
					set = function(info, r, g, b, a)
						E_EM.MainFrame:SetBackdropColor(r, g, b, a)
						E_EM.header:SetBackdropColor(r, g, b, a)
						E_EM.footer:SetBackdropColor(r, g, b, a)
						E_EM.db.global.colorbg.r = r;
						E_EM.db.global.colorbg.g = g;
						E_EM.db.global.colorbg.b = b;
						E_EM.db.global.colorbg.a = a;
					end,
					order = 8, 
				},
				color = { 
					type = 'color',
					name = L["Color de la barra"],
					desc = L["Cambiar color de la barra y opacidad."],
					hasAlpha = true,
					get = function(info) return E_EM.db.global.color.r, E_EM.db.global.color.g, E_EM.db.global.color.b, E_EM.db.global.color.a end,
					set = function(info, r, g, b, a)
						for i=1,#E_EM.bars do
							E_EM.bars[i]:SetStatusBarColor(r, g, b, a)
						end 			
						E_EM.db.global.color.r = r;
						E_EM.db.global.color.g = g;
						E_EM.db.global.color.b = b;
						E_EM.db.global.color.a = a;
					end,
					order = 9, 
				},
				classcolor = {
					type = "toggle",
					name = L["Colores por clase"],
					desc = " ",
					order = 12,
					get = function() return E_EM.db.global.classcolor end,
					set = function()
						E_EM.db.global.classcolor = not E_EM.db.global.classcolor
						if not E_EM.db.global.classcolor then 
							for i=1, #E_EM.bars do
								E_EM.bars[i]:SetStatusBarColor(dbd.color.r, dbd.color.g, dbd.color.b, dbd.color.a)
							end
						end
						E_EM:UpdateFrame()
					end,
				},
				texcolor = { 
					type = 'color',
					name = L["Font color"],
					desc = L["Cambiar color de fuente y opacidad."],
					hasAlpha = true,
					get = function(info) return E_EM.db.global.texcolor.r, E_EM.db.global.texcolor.g, E_EM.db.global.texcolor.b, E_EM.db.global.texcolor.a end,
					set = function(info, r, g, b, a)
						for i=1,#E_EM.bars do
							E_EM.LeftText[i]:SetTextColor(r,g,b,a)
							E_EM.RightText[i]:SetTextColor(r,g,b,a)		
							E_EM.Index[i]:SetTextColor(r,g,b,a)																																			
						end																 
						E_EM.db.global.texcolor.r = r;
						E_EM.db.global.texcolor.g = g;
						E_EM.db.global.texcolor.b = b;
						E_EM.db.global.texcolor.a = a;
					end,
					order = 10, 
				},	
				bordercolor = { 
					type = 'color',
					name = L["Color de Borde"],
					desc = L["Cambiar color de borde."],
					hasAlpha = true,
					get = function(info) return E_EM.db.global.colorborder.r, E_EM.db.global.colorborder.g, E_EM.db.global.colorborder.b, E_EM.db.global.colorborder.a end,
					set = function(info, r, g, b, a)
						E_EM.MainFrame:SetBackdropBorderColor(r, g, b, a)	
						E_EM.header:SetBackdropBorderColor(r, g, b, a)
						E_EM.footer:SetBackdropBorderColor(r, g, b, a)						 
						E_EM.db.global.colorborder.r = r;
						E_EM.db.global.colorborder.g = g;
						E_EM.db.global.colorborder.b = b;
						E_EM.db.global.colorborder.a = a;
					end,
					order = 11, 
				},							 												 
				font = {
					type = "select",
					name = L["Fuente"],
					desc = L["Cambiar la fuente."],
					values = fonts,
					get = function(info) return GetLSMIndex("font", E_EM.db.global.font) end,
					set = function(info, v)
						E_EM.db.global.font = media:List("font")[v]
						for i=1,#E_EM.bars do
							E_EM.LeftText[i]:SetFont(media:Fetch("font",E_EM.db.global.font),E_EM.db.global.fontsize)
							E_EM.RightText[i]:SetFont(media:Fetch("font",E_EM.db.global.font),E_EM.db.global.fontsize)
							E_EM.Index[i]:SetFont(media:Fetch("font",E_EM.db.global.font),E_EM.db.global.fontsize)																																				
						end
					end,
					order = 12,
				},		
				bartex = {
					type = "select",
					name = L["Textura de la barra"],
					desc = L["Cambiar la textura de la barra."],
					values = textures,
					get = function(info) return GetLSMIndex("statusbar", E_EM.db.global.tex) end,
					set = function(info, v)
						E_EM.db.global.tex = media:List("statusbar")[v]
						for i=1,#E_EM.bars do
							E_EM.bars[i]:SetStatusBarTexture(media:Fetch("statusbar",E_EM.db.global.tex))
						end 
					end,
					order = 13,
				},	
				strata = {
					type = "select",
					name = "Relleno de Frame",
					desc = " ",
					values = {["TOOLTIP"] = "1. Tooltip", ["FULLSCREEN_DIALOG"] = "2. Fullscreen Dialog", ["FULLSCREEN"] = "3. Fullscreen", ["DIALOG"] = "4. Dialog", ["HIGH"] = "5. High", ["MEDIUM"] = "6. Medium", ["LOW"] = "7. Low", ["BACKGROUND"] = "8. Background"},
					get = function(info) return E_EM.db.global.strata end,
					set = function(info, v)
						E_EM.db.global.strata=v
						E_EM.MainFrame:SetFrameStrata(v)
					end,
					order = 14,
				},	
				scaler = { 
					type = 'range',
					name = L["Escala"],
					desc = " ",
					min = 0.3,
					max = 2,
					step = 0.01,
					get = function() return E_EM.db.global.scale end,
					set = function(info, v) 
						E_EM.db.global.scale = v;						
						E_EM.MainFrame:SetScale(v)									 
					end, 
					order = 6, 
				},					
				scrollbar = {
					type = "toggle",
					name = "Barra de Scroll",
					desc = " ",
					order = 2,
					get = function() return E_EM.db.global.scrollbar end,
					set = function()
						E_EM.db.global.scrollbar = not E_EM.db.global.scrollbar
						if E_EM.db.global.scrollbar==true then 
							E_EMMainFrameScrollBar:SetScale(0.7)
							for i=1,#E_EM.bars do
								E_EM.bars[i]:SetWidth(E_EM.header:GetWidth()-27)
							end
						else 
							E_EMMainFrameScrollBar:SetScale(0.00000001)
							for i=1,#E_EM.bars do
								E_EM.bars[i]:SetWidth(E_EM.header:GetWidth()-15)
							end							
						end
					end,
				},																				 
			},					 						 
		},		
	}, 
} 

AceConfig = LibStub("AceConfigDialog-3.0")
LibStub("AceConfig-3.0"):RegisterOptionsTable("E_EM", E_EM.options, nil)
AceConfig:AddToBlizOptions("E_EM", "GPLP Statistic Meter")